/*
 * lib2Desa - Another 2D game library in C++
 * Copyright (C) 2009 Erivaldo Xavier de Lima Filho
 *
 * This program is free software and is distributed under GNU General Public License version 3. You can modify and/or redistribute
 * it under the terms of GPL version 3 or posterior version. This program comes with NO WARRANTY, for more details, see LICENSE,
 * or <http://www.gnu.org/licenses>.
 *
 * Contact info:
 *
 *	Erivaldo Xavier de Lima Filho, desadoc@gmail.com
 *
 */

#include "Core/Event.hpp"
#include "Core/CollisionMessage.hpp"
#include "Ext/CollisionSimpleRule.hpp"
#include "Core/GeomUtil.hpp"

#ifndef _NO_NAMESPACE
namespace lib2Desa {
#endif

void CollisionSimpleRule::processGroup(shared_ptr<GameObjectGroup> g)
{
	std::list<shared_ptr<GameObject> >& objects = g->getObjects();
	std::list<shared_ptr<GameObject> >::iterator it = objects.begin();
	std::list<shared_ptr<GameObject> >::iterator it2;
	shared_ptr<IEvent> evt;
	Rect r;

	for(; it != objects.end(); it++)
	{
		it2 = it;
		it2++;
		for(; it2 != objects.end(); it2++)
		{
			r = collision((*it)->getRect(), (*it2)->getRect());
			if(r.area() > 0)
			{
				evt = new Event<CollisionMessage>(*it2,*it,CollisionMessage(r));
				(*it)->notify(evt);
				evt = new Event<CollisionMessage>(*it,*it2,CollisionMessage(r));
				(*it2)->notify(evt);
			}
		}
	}
}

void CollisionSimpleRule::processEvents(void)
{
	std::list<shared_ptr<GameObjectGroup> >::iterator it = groups->begin();

	for(; it != groups->end(); it++)
	{
		processGroup(*it);
	}
}

#ifndef _NO_NAMESPACE
}
#endif
